import { world } from "@minecraft/server";
import { GamePlayer } from "@sapi-game/gamePlayer/gamePlayer";
import { PlayerGroup } from "@sapi-game/gamePlayer/playerGroup";
import { GameState } from "@sapi-game/main";
import { ScriptRunner } from "@sapi-game/Runner/scriptRunner";

export class KcpdWatingState<T extends GamePlayer> extends GameState<
    T,
    any,
    { players: PlayerGroup<T>; next: () => void }
> {
    get players() {
        return this.config!.players;
    }

    override onEnter(): void {
        if (!this.config) return;
        this.players.forEach((p) =>
            p.player.teleport({ x: -150.3, y: -53.0, z: -368.6 })
        );
        world.gameRules.pvp = true;
        this.runner.run(this.countDown.bind(this));
    }

    async countDown(r: ScriptRunner) {
        let pitch = 0.5;
        for (let i = 5; i > 0; i--) {
            let color = i == 3 ? "§e" : i < 3 ? "§4" : "§a";
            this.players.title(color + i, "§e游戏即将开始");
            this.players.playSound("note.pling", { pitch: pitch });
            pitch += 0.1;
            await r.wait(20);
        }
        //下一步
        this.config!.next();
    }
}
